package com.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.service.edu.mapper.CourseDescriptionMapper;
import com.service.edu.pojo.Course;
import com.service.edu.mapper.CourseMapper;
import com.service.edu.pojo.CourseDescription;
import com.service.edu.pojo.vo.CourseFrontVo;
import com.service.edu.pojo.vo.CourseInfoVo;
import com.service.edu.pojo.vo.CourseQuery;
import com.service.edu.service.CourseDescriptionService;
import com.service.edu.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.edu.service.SubjectService;
import com.service.edu.service.TeacherService;
import com.service.utils.ResultVo;
import org.apache.poi.ss.formula.functions.BaseNumberUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-09-28
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseDescriptionService courseDescriptionService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private SubjectService subjectService;

    @Autowired
    private CourseMapper courseMapper;


    @Override
    @Transactional
    public String saveCourse(CourseInfoVo courseInfoVo) {
        //初始化course对象
        Course course = new Course();
        //将courseInfoVo与course相同的属性 赋值给course
        BeanUtils.copyProperties(courseInfoVo,course);
        course.setGmtCreate(new Date());
        course.setGmtModified(new Date());
        //添加课程信息
        boolean save = save(course);
        //判断 当前课程添加成功才可以添加课程详细信息
        if (save){
            CourseDescription courseDescription= new CourseDescription();
            courseDescription.setDescription(courseInfoVo.getDescription());
            courseDescription.setGmtCreate(new Date());
            courseDescription.setGmtModified(new Date());
            courseDescription.setId(course.getId());
            courseDescriptionService.save(courseDescription);
        }
        return course.getId();
    }

    @Override
    @Transactional
    public String updateCourse(CourseInfoVo courseInfoVo) {
        //初始化course对象
        Course course = new Course();
        //将courseInfoVo与course相同的属性 赋值给course
        BeanUtils.copyProperties(courseInfoVo,course);
        course.setGmtModified(new Date());
        //修改课程信息
        boolean b = updateById(course);
        //判断 当前课程添加成功才可以添加课程详细信息
        if (b){
            CourseDescription courseDescription= new CourseDescription();
            courseDescription.setDescription(courseInfoVo.getDescription());
            courseDescription.setGmtModified(new Date());
            courseDescription.setId(course.getId());
            courseDescriptionService.updateById(courseDescription);
        }
        return course.getId();
    }

    @Override
    public Map<String, Object> queryCourseVo(String id) {
        Map<String, Object> map = new HashMap<>();
        Course course = getById(id);
        map.put("title",course.getTitle());
        map.put("lessonNum",course.getLessonNum());
        map.put("price",course.getPrice());
        map.put("teacherName",teacherService.getById(course.getTeacherId()).getName());
        map.put("subjectOneName",subjectService.getById(course.getSubjectId()).getTitle());
        map.put("subjectTwoName",subjectService.getById(course.getSubjectParentId()).getTitle());
        map.put("cover",course.getCover());
        return map;
    }

    @Override
    public ResultVo pages(CourseQuery courseQuery) {
        //初始化条件构造器
        QueryWrapper queryWrapper = new QueryWrapper();
        //分页
        Page<Course> page = new Page<>(courseQuery.getPageNum(),courseQuery.getRows());
        if (courseQuery==null){
            this.page(page,null);
        }else{
            //如果title不为空的话 就添加模糊查询title的条件
            if (!StringUtils.isEmpty(courseQuery.getTitle())){
                queryWrapper.like("title",courseQuery.getTitle());
            }
            if (!StringUtils.isEmpty(courseQuery.getStatus())){
                queryWrapper.eq("status",courseQuery.getStatus());
            }
        }

        //想把逻辑删除后的数据也查询出来
        queryWrapper.last("or is_deleted=1");
        page(page,queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("rows",page.getSize());
        map.put("pageNum",page.getCurrent());
        map.put("items",page.getRecords());
        return ResultVo.ok().data(map);

    }

    @Override
    public int updateIsDelete(String id) {
        return courseMapper.updateIsDelete(id);
    }

    @Override
    public ResultVo pageCourse(Integer pageNum, Integer rows, CourseFrontVo courseFrontVo) {
        //初始化条件构造器
        QueryWrapper queryWrapper = new QueryWrapper();
        //分页
        Page<Course> page = new Page<>(pageNum,rows);
        if (courseFrontVo==null){
            this.page(page,null);
        }else{

            if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){
                queryWrapper.like("subject_parent_id",courseFrontVo.getSubjectParentId());
            }
            if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())){
                queryWrapper.like("subject_id",courseFrontVo.getSubjectId());
            }
            if (courseFrontVo.getPriceSort()){
                queryWrapper.orderByDesc("price");
            }
            if (courseFrontVo.getGmtCreateSort()){
                queryWrapper.orderByDesc("gmt_create");
            }
            page(page,queryWrapper);

        }


        Map<String,Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("rows",page.getSize());
        map.put("pages",page.getPages());
        map.put("pageNum",page.getCurrent());
        map.put("items",page.getRecords());
        return ResultVo.ok().data(map);
    }

    @Override
    public int updateViewsCount(String id) {
        return courseMapper.updateViewsCount(id);

    }

    @Override
    public Integer queryCreateCount(String day) {
        return courseMapper.queryCreateCount(day);
    }


}
